package org.ielts.service;

import java.net.URL;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.List;

import javax.xml.rpc.ServiceException;

import org.ielts.domain.Sex;
import org.ielts.domain.base.SignCourse;
import org.ielts.domain.base.Student;
import org.ielts.domain.report.StudentQuery;
import org.ielts.domain.security.User;
import org.ielts.repository.SignCourseRepository;
import org.ielts.repository.StudentRepository;
import org.ielts.util.figner.FignerService;
import org.ielts.util.figner.FignerServiceLocator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yifeng.page.table.Paginations;

@Service
public class StudentService {
	FignerService fignerService = new FignerServiceLocator();

	@Autowired
	StudentRepository studentRepository;

	@Autowired
	private SignCourseRepository signCourseRepository;

	@Transactional
	public Student modifyStudent(String id, String name, String title, String stuId) {
		Student student = studentRepository.findOne(id);

		student.setName(name);
		student.setTitle(title);
		student.setStuId(stuId);

		return student;
	}

	@Transactional
	public Student createStudent(String name, String title) {
		Student student = new Student();
		student.setName(name);
		student.setTitle(title);

		studentRepository.save(student);

		return student;
	}

	@Transactional
	public Student createStudent(String name) {

		return createStudent(name, name);
	}

	@Transactional
	public Student findByNo(String no) {

		Student student = studentRepository.findByNo(no);
		return student;
	}

	@Transactional
	public Iterable<Student> findByName(String name) {
		Iterable<Student> students = studentRepository.findByName(name);
		return students;
	}

	@Transactional
	public Student findOne(String id) {
		Student student = studentRepository.findOne(id);
		return student;
	}

	@Transactional
	public Page<Student> findAll(Pageable pageable) {
		Page<Student> students = studentRepository.findAll(pageable);

		return students;
	}

	@Transactional
	public Page<Student> findConsultStarred(Pageable pageable) {
		Page<Student> students = studentRepository.findConsultStarred(pageable, true);

		return students;
	}

	@Transactional
	public Page<Student> findConsultUnstarred(Pageable pageable) {
		Page<Student> students = studentRepository.findConsultStarred(pageable, false);

		return students;
	}

	@Transactional
	public Page<Student> findAll(StudentQuery studentQuery, Pageable pageable) {
		Page<Student> students = studentRepository.findConsultStarred(studentQuery, pageable, false);

		return students;
	}

	@Transactional
	public Iterable<Student> findByStuId(String stuId) {
		Iterable<Student> students = studentRepository.findByStuId(stuId);
		return students;
	}

	@Transactional
	public Iterable<Student> findByPinyin(String pinyin) {
		Iterable<Student> students = studentRepository.findByPinyin(pinyin);
		return students;
	}

	@Transactional
	public void initStudentPinyin(String id) {
		Student stu = studentRepository.findOne(id);
		String pinyin = Student.getPinYinHeadChar(id);
		stu.setPinyin(pinyin);
	}

	@Transactional
	public Student modifyStudent(String id, String name, Sex sex, String title, String email, Date birthday,
			String job, String address, String phone, String zip, String mobile, String education, String stuId) {
		Student stu = studentRepository.findOne(id);
		stu.setName(name);
		stu.setBirthday(birthday);
		stu.setJob(job);
		stu.setAddress(address);
		stu.setPhone(phone);
		stu.setZip(zip);
		stu.setMobile(mobile);
		stu.setEducation(education);
		stu.setStuId(stuId);
		stu.setSex(sex);
		stu.setTitle(title);
		stu.setEmail(email);

		return stu;
	}

	@Transactional
	public Student createStudent(Student student) {
		student = studentRepository.save(student);

		return student;
	}

	@Transactional
	public Student changeStuId(String id, String stuId) {
		Student stu = studentRepository.findOne(id);
		stu.setStuId(stuId);

		return stu;
	}

	@Transactional
	public Page<Student> findByCurseRemainTimes(int times, double rate, String[] ccnames, Pageable pageable) {
		signCourseRepository.syncUsedTimes();
		return studentRepository.findByCurseRemainTimes(times, rate, ccnames, pageable);
	}

	@Transactional
	public Page<Student> findByCurseRemainDate(int day, double rate, String[] ccnames, Pageable pageable) {
		signCourseRepository.syncUsedTimes();
		return studentRepository.findByCurseRemainDate(day, rate, ccnames, pageable);
	}

	@Transactional
	public Iterable<Student> findByMobile(String mobile) {
		Iterable<Student> students = studentRepository.findByMobile(mobile);
		return students;
	}

	@Transactional
	public Student save(Student student) {
		return studentRepository.save(student);
	}

	@Transactional
	public boolean existsName(String name) {
		return studentRepository.existsName(name);
	}

	@Transactional
	public Student findByNameAndStuId(String name, String stuId) {
		Student student = studentRepository.findByNameAndStuId(name, stuId);
		return student;
	}

	public boolean registerFigner(String stuId, String fignerTemplateStr) throws Exception {
		return fignerService.getFignerServiceSoap(new URL("http://localhost:1105")).regFgTemplate(stuId,
				fignerTemplateStr);
	}

	public String identificationFigner(String fignerTemplateStr) throws Exception {
		return fignerService.getFignerServiceSoap(new URL("http://localhost:1105")).identification(fignerTemplateStr);
	}
}
